﻿using Newtonsoft.Json;
using QunProject.Common;
using System.Web;
using System.Web.Mvc;

namespace QunProject.Web.Controllers
{
    /// <summary>
    /// 账户
    /// </summary>
    public class MemberController : BaseController
    {
        //个人
        public ActionResult Index()
        {
            return View();
        }
        //个人资料
        public ActionResult Info()
        {
            return View();
        }
        //安全中心
        public ActionResult Safety()
        {
            return View();
        }
        //我的文章
        public ActionResult Article()
        {
            return View();
        }
        //我的讨论
        public ActionResult Discuss()
        {
            return View();
        }
        //我的专题
        public ActionResult Special()
        {
            return View();
        }
        //我的回复
        public ActionResult Reply()
        {
            return View();
        }
        //编辑文章
        public ActionResult ArticleEdit(int? id)
        {
            ViewBag.ArticleID = id;
            return View();
        }
        //编辑专题
        public ActionResult SpecialEdit(int? id)
        {
            ViewBag.SpecialID = id;
            return View();
        }
        //专题文章
        public ActionResult SpecialArticle(int? id)
        {
            ViewBag.SpecialID = id;
            return View();
        }
        //编辑专题文章
        public ActionResult SpecialArticleEdit(int? id, int? attach)
        {
            ViewBag.SpecialID = id;
            ViewBag.ArticleID = attach;
            return View();
        }
        //编辑讨论
        public ActionResult DiscussEdit(int? id)
        {
            ViewBag.discussId = id;
            return View();
        }

        /// <summary>
        /// 获取个人中心数据
        /// </summary>
        /// <returns></returns>
        public JsonResult GetMemberIndexResult()
        {
            var result = HttpPost("GetMemberIndexResult");
            return Json(result);
        }

        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="model">用户实体</param>
        /// <returns></returns>
        public JsonResult UserEditInfoResult(Model.member model)
        {
            var paras = new { model };
            var result = HttpPost("UserEditInfoResult", JsonConvert.SerializeObject(paras));
            var resultObject = JsonConvert.DeserializeObject<Result>(result);
            if (resultObject.succ)
            {
                RefreshLoginMember();
            }
            return Json(result);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="pwd">旧密码</param>
        /// <param name="pwd1">新密码</param>
        /// <param name="pwd2">重复新密码</param>
        /// <returns></returns>
        public JsonResult UserEditPasswordResult(string pwd = "", string pwd1 = "", string pwd2 = "")
        {
            var paras = WeaveParameters<MemberController>("UserEditPasswordResult");
            var result = HttpPost("UserEditPasswordResult", JsonConvert.SerializeObject(paras));
            return Json(result);
        }

        /// <summary>
        /// 用户获取自己的文章
        /// </summary>
        /// <param name="page">页索引</param>
        /// <param name="limit">页大小</param>
        /// <returns></returns>
        public JsonResult GetMemberArticleListResult(int page = 1, int limit = 10)
        {
            var paras = WeaveParameters<MemberController>("GetMemberArticleListResult");
            var result = HttpPost("GetMemberArticleListResult", JsonConvert.SerializeObject(paras));
            return Json(result);
        }

        /// <summary>
        /// 用户删除自己的一个文章
        /// </summary>
        /// <param name="articleId">文章ID</param>
        /// <returns></returns>
        public JsonResult DeleteMemberArticleResult(int articleId)
        {
            var paras = WeaveParameters<MemberController>("DeleteMemberArticleResult");
            var result = HttpPost("DeleteMemberArticleResult", JsonConvert.SerializeObject(paras));
            return Json(result);
        }

        /// <summary>
        /// 用户获取自己的一个文章
        /// </summary>
        /// <param name="articleId">文章ID</param>
        /// <returns></returns>
        public JsonResult GetMemberArticleResult(int articleId)
        {
            var paras = WeaveParameters<MemberController>("GetMemberArticleResult");
            var result = HttpPost("GetMemberArticleResult", JsonConvert.SerializeObject(paras));
            return Json(result);
        }

        /// <summary>
        /// 编辑文章
        /// </summary>
        /// <param name="model">文章实体</param>
        /// <param name="resJson">资源Json</param>
        /// <returns></returns>
        [ValidateInput(false)]
        public JsonResult UserEditArticleResult(Model.article model, string resJson = "")
        {
            model.Content = model.Content.StringToUnicode();
            //model.Content = model.Content.Replace("\"", "'");
            //model.Content = HttpUtility.UrlEncode(model.Content);
            //model.Content = model.Content.Replace("+", "[ad]");
            //model.Content = model.Content.Replace("=", "[eq]");
            var paras = new { model, resJson };
            var result = HttpPost("UserEditArticleResult", JsonConvert.SerializeObject(paras));
            return Json(result);
        }

        /// <summary>
        /// 保存文章草稿
        /// </summary>
        /// <param name="model">文章实体</param>
        /// <param name="resJson">资源Json</param>
        /// <returns></returns>
        [ValidateInput(false)]
        public JsonResult UserEditArticleDraftResult(Model.article model, string resJson = "")
        {
            model.Content = model.Content.StringToUnicode();
            //model.Content = model.Content.Replace("\"", "'");
            //model.Content = HttpUtility.UrlEncode(model.Content);
            //model.Content = model.Content.Replace("+", "[ad]");
            //model.Content = model.Content.Replace("=", "[eq]");
            var paras = new { model, resJson };
            var result = HttpPost("UserEditArticleDraftResult", JsonConvert.SerializeObject(paras));
            return Json(result);
        }

        /// <summary>
        /// 用户获取自己的专题
        /// </summary>
        /// <param name="page">页索引</param>
        /// <param name="limit">页大小</param>
        /// <returns></returns>
        public JsonResult GetMemberSpecialListResult(int page = 1, int limit = 10)
        {
            var paras = WeaveParameters<MemberController>("GetMemberSpecialListResult");
            var result = HttpPost("GetMemberSpecialListResult", JsonConvert.SerializeObject(paras));
            return Json(result);
        }

        /// <summary>
        /// 用户获取自己的一个专题
        /// </summary>
        /// <param name="specialId">专题ID</param>
        /// <returns></returns>
        public JsonResult GetMemberSpecialResult(int specialId)
        {
            var paras = WeaveParameters<MemberController>("GetMemberSpecialResult");
            var result = HttpPost("GetMemberSpecialResult", JsonConvert.SerializeObject(paras));
            return Json(result);
        }

        /// <summary>
        /// 用户获取自己的一个专题文章列表
        /// </summary>
        /// <param name="specialId">专题ID</param>
        /// <param name="page">页索引</param>
        /// <param name="limit">页大小</param>
        /// <returns></returns>
        public JsonResult GetMemberSpecialArticleResult(int specialId, int page = 1, int limit = 10)
        {
            var paras = WeaveParameters<MemberController>("GetMemberSpecialArticleResult");
            var result = HttpPost("GetMemberSpecialArticleResult", JsonConvert.SerializeObject(paras));
            return Json(result);
        }

        /// <summary>
        /// 编辑专题
        /// </summary>
        /// <param name="model">专题实体</param>
        /// <returns></returns>
        [ValidateInput(false)]
        public JsonResult UserEditSpecialResult(Model.special model)
        {
            model.Content = model.Content.StringToUnicode();
            //model.Content = model.Content.Replace("\"", "'");
            //model.Content = HttpUtility.UrlEncode(model.Content);
            //model.Content = model.Content.Replace("+", "[ad]");
            //model.Content = model.Content.Replace("=", "[eq]");
            var paras = new { model };
            var result = HttpPost("UserEditSpecialResult", JsonConvert.SerializeObject(paras));
            return Json(result);
        }

        /// <summary>
        /// 用户删除自己的一个专题
        /// </summary>
        /// <param name="specialId">专题ID</param>
        /// <returns></returns>
        public JsonResult DeleteMemberSpecialResult(int specialId)
        {
            var paras = WeaveParameters<MemberController>("DeleteMemberSpecialResult");
            var result = HttpPost("DeleteMemberSpecialResult", JsonConvert.SerializeObject(paras));
            return Json(result);
        }

        /// <summary>
        /// 用户获取自己的讨论
        /// </summary>
        /// <param name="page">页索引</param>
        /// <param name="limit">页大小</param>
        /// <returns></returns>
        public JsonResult GetMemberDiscussListResult(int page = 1, int limit = 10)
        {
            var paras = WeaveParameters<MemberController>("GetMemberDiscussListResult");
            var result = HttpPost("GetMemberDiscussListResult", JsonConvert.SerializeObject(paras));
            return Json(result);
        }

        /// <summary>
        /// 编辑讨论
        /// </summary>
        /// <param name="model">讨论实体</param>
        /// <returns></returns>
        [ValidateInput(false)]
        public JsonResult UserEditDiscussResult(Model.article model)
        {
            model.Content = model.Content.StringToUnicode();
            //model.Content = model.Content.Replace("\"", "'");
            //model.Content = HttpUtility.UrlEncode(model.Content);
            //model.Content = model.Content.Replace("+", "[ad]");
            //model.Content = model.Content.Replace("=", "[eq]");
            var paras = new { model };
            var result = HttpPost("UserEditDiscussResult", JsonConvert.SerializeObject(paras));
            return Json(result);
        }

        /// <summary>
        /// 用户获取自己的回复
        /// </summary>
        /// <param name="page">页索引</param>
        /// <param name="limit">页大小</param>
        /// <returns></returns>
        public JsonResult GetMemberReplyListResult(int page = 1, int limit = 10)
        {
            var paras = WeaveParameters<MemberController>("GetMemberReplyListResult");
            var result = HttpPost("GetMemberReplyListResult", JsonConvert.SerializeObject(paras));
            return Json(result);
        }
    }
}